
-- 数据统计
DS_Baozou   = 0x01 --暴走(次数=1)
DS_Score    = 0x02 --获得分数(分数)
DS_Crystal  = 0x03 --水晶币增加(数量)
DS_BuyCrystal  = 0x04 --购买水晶币(次数=1)
DS_Combo    = 0x05 --连接次数(次数)
DS_Skill    = 0x06 --击杀敌人(数量)
DS_ModeOp   = 0x07 --开启模式(模式)
DS_ModeEnd  = 0x08 --通关模式(模式)
DS_ModePlay = 0x09 --进行模式(模式)
DS_GameTime = 0x0a --游戏时间累计(时间-秒)
DS_HunJing  = 0x0b --消除魂晶累计(数量)
DS_UseBom   = 0x0c --炸弹使用(类型)
DS_WeiBo    = 0x0d --发微博(次数=1)
DS_ClickNil = 0x0e --无聊点击(次数=1)
DS_Tortal   = 0x0f --教程完成
DS_Tili     = 0x10 --体力消耗(数值=1)


local function Func_DS_Baozou(v)
    if v == nil then
        v = 1
    end
    SetAchieves(DS_Baozou, GetAchieves(DS_Baozou) + v)
end
local function Func_DS_Score(v)
    local lastScore = GetAchieves(DS_Score)
    
    if v == nil then
        v = 0
    end
    SetAchieves(DS_Score, GetAchieves(DS_Score) + v)
    
    --- 赠送物品奖励**积分为10000的倍数(递增
    local score = 10000
    while true do
        if lastScore < score then
            if GetAchieves(DS_Score) >= score then
                -- 获得
                local msg = string.format("积分达到 %d", score)
                GFunc_ZhenSong(msg)
            else
                break
            end
        else
            break
        end
        score = score * 2
    end
end

local function Func_DS_Crystal(v)
    if v == nil then
        v = 0
    end
    SetAchieves(DS_Crystal, GetAchieves(DS_Crystal) + v)
end
local function Func_DS_BuyCrystal(v)
    if v == nil then
        v = 1
    end
    SetAchieves(DS_BuyCrystal, GetAchieves(DS_BuyCrystal) + v)
end
local function Func_DS_Combo(v)
    if v == nil then
        v = 0
    end
    --超过才更新
    if v > GetAchieves(DS_Combo) then
        SetAchieves(DS_Combo, v)
    end
end
local function Func_DS_Skill(v)
    local lastKills = GetAchieves(DS_Skill)
    
    if v == nil then
        v = 0
    end
    SetAchieves(DS_Skill, GetAchieves(DS_Skill) + v)
    
    
    --- 赠送物品奖励**魂力为1000的倍数(递增
    local score = 1000
    while true do
        if lastKills < score then
            if GetAchieves(DS_Skill) >= score then
                -- 获得
                local msg = string.format("击杀敌人达到 %d", score)
                GFunc_ZhenSong(msg)
            else
                break
            end
        else
            break
        end
        score = score * 2
    end
end

local function Func_DS_ModeOp(v)
    SetAchieves(table.concat({DS_ModeOp,"_",v}), 1)
end
local function Func_DS_ModeEnd(v)
    SetAchieves(table.concat({DS_ModeEnd,"_",v}), 1)
end
local function Func_DS_ModePlay(v)
    SetAchieves(table.concat({DS_ModePlay,"_",v}), 1)
end
local function Func_DS_UseBom(v)
    if v == nil then
        v = 1
    end
    local achn = table.concat({DS_UseBom,"_",v})
    SetAchieves(achn, GetAchieves(achn) + v)
end

local function Func_DS_GameTime(v)
    if v == nil then
        v = 0
    end
    SetAchieves(DS_GameTime, GetAchieves(DS_GameTime) + v)
    --print(GetAchieves(DS_GameTime), v)
end
local function Func_DS_HunJing(v)
    if v == nil then
        v = 0
    end
    SetAchieves(DS_HunJing, GetAchieves(DS_HunJing) + v)
end
local function Func_DS_WeiBo(v)
    if v == nil then
        v = 1
    end
    SetAchieves(DS_WeiBo, GetAchieves(DS_WeiBo) + v)
end
local function Func_DS_ClickNil(v)
    if v == nil then
        v = 1
    end
    SetAchieves(DS_ClickNil, GetAchieves(DS_ClickNil) + v)
end
local function Func_DS_Tortal(v)
    if v == nil then
        v = 1
    end
    SetAchieves(DS_Tortal, GetAchieves(DS_Tortal) + v)
end
local function Func_DS_Tili(v)
    if v == nil then
        v = 1
    end
    SetAchieves(DS_Tili, GetAchieves(DS_Tili) + v)
end

local FuncTable = {
    [DS_Baozou]=Func_DS_Baozou,
    [DS_Score]=Func_DS_Score,
    [DS_Crystal]=Func_DS_Crystal,
    [DS_BuyCrystal]=Func_DS_BuyCrystal,
    [DS_Combo]=Func_DS_Combo,
    [DS_Skill]=Func_DS_Skill,
    [DS_ModeOp]=Func_DS_ModeOp,
    [DS_ModeEnd]=Func_DS_ModeEnd,
    [DS_ModePlay]=Func_DS_ModePlay,
    [DS_GameTime]=Func_DS_GameTime,
    [DS_HunJing]=Func_DS_HunJing,
    [DS_UseBom]=Func_DS_UseBom,
    [DS_WeiBo]=Func_DS_WeiBo,
    [DS_ClickNil]=Func_DS_ClickNil,
    [DS_Tortal]=Func_DS_Tortal,
    [DS_Tili]=Func_DS_Tili,
}

function Func_DataStatis(tpe, value)
    if FuncTable[tpe] ~= nil then
        FuncTable[tpe](value)
    end
    
    --有成就解锁,显示成就解锁界面
    --判断所有此类别没有解锁的成就
    if _AchieveTable_lock[tpe] ~= nil then
        local hasUnlock = false
        --此类对此类未解锁的成就判定解锁
        for k, ach in pairs(_AchieveTable_lock[tpe]) do
            --是否可解锁
            if GFunc_AchieveJuge(ach) then
                --可解锁
                --显示成就获得界面,
                GFunc_CreateShowAchieve(ach)
                
                --从未解锁成就中删除
                _AchieveTable_lock[tpe][k] = nil
                
                hasUnlock = true
                
                ---添加至已解锁表中
                _AchieveTable_Unlock[ach] = true
            end
        end
        
        if hasUnlock then
            --如果此类别没有成就了,则删除此类别
            local hasl = false
            table.foreach(_AchieveTable_lock[tpe], function(k,ach)
                if ach ~= nil then
                    hasl = true
                end
            end)
            if not hasl then
                _AchieveTable_lock[tpe] = nil
            end
        end
    end
end


--获得未解锁与已解锁的成就
_AchieveTable_Unlock = {}  --解锁状态
_AchieveTable_lock = {} --未解锁(类别,成就ID
function Load_AllAchieves()
    table.foreach(Achieve_Infos, function(v, info)
        _AchieveTable_Unlock[v] = GFunc_AchieveJuge(v)
        
        if not _AchieveTable_Unlock[v] then
            if _AchieveTable_lock[info[PType]] == nil then
                _AchieveTable_lock[info[PType]] = {}
            end
            table.insert(_AchieveTable_lock[info[PType]], v)
        end
    end)
end


--成就判定解锁
GFunc_AchieveJuge = function(_ach)
    local value = Achieve_Infos[_ach][PCount]
    if value == nil then
        value = 1
    end
    
    --已有数值
    local achName = Achieve_Infos[_ach][PType]
    if Achieve_Infos[_ach][PType] == DS_UseBom then
        achName = table.concat({DS_UseBom,"_",Achieve_Infos[_ach][PModel]})
    elseif Achieve_Infos[_ach][PType] == DS_ModeOp then
        achName = table.concat({DS_ModeOp,"_",Achieve_Infos[_ach][PCount]})
        value = 1
    elseif Achieve_Infos[_ach][PType] == DS_ModeEnd then
        achName = table.concat({DS_ModeEnd,"_",Achieve_Infos[_ach][PCount]})
        value = 1
    elseif Achieve_Infos[_ach][PType] == DS_ModePlay then
        achName = table.concat({DS_ModePlay,"_",Achieve_Infos[_ach][PCount]})
        value = 1
    end
    local achValue = GetAchieves(achName)
    
    if achValue >= value then
        return true
    end
    
    return false
end


---当前显示的成就窗口数量
local AchFormSums = 0

GFunc_CreateShowAchieve = function(ach)
    --13/2/22取消显示方式
    local achInfo = Achieve_Infos[ach]
    --
    --if achInfo == nil then
    --    return
    --end
    --
    --AchFormSums = AchFormSums + 1
    --
    --
    --local layer = CCLayerColor:create(ccc4(0,0,0,0))
    --local menu = CCMenu:create()
    ---- 主菜单大小
    --menu:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    ---- 设置坐标
    --menu:setPosition(0, 0)
    ---- 添加至主菜单层
    --layer:addChild(menu, 5)
    --
    --
    --local btnLock = true
    --local function unLockBtn()
    --    btnLock = false
    --    --print("unlock")
    --    
    --    AchFormSums = AchFormSums - 1
    --    CCDirector:sharedDirector():getRunningScene():removeChild(layer, true)
    --    if AchFormSums == 0 then
    --        --_GameEndRun = false
    --    end
    --end
    --
    --local function callbackmenu()
    --    if btnLock then
    --        return
    --    end
    --    
    --    AchFormSums = AchFormSums - 1
    --    CCDirector:sharedDirector():getRunningScene():removeChild(layer, true)
    --    if AchFormSums == 0 then
    --        _GameEndRun = false
    --    end
    --end
    ----遮罩
    ----local btnItem = nil
    ----btnItem = CCMenuItemLabel:create(CCLabelTTF:create("", "Arial", 24))
    ----btnItem:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    ----btnItem:registerScriptTapHandler(callbackmenu)
    ----btnItem:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    ----menu:addChild(btnItem,100)
    --
    ----背景
    --local px = DWinSizeWidth/2
    --local py = DWinSizeHeight/2 + 120
    --local _bgBrick = CCSprite:createWithSpriteFrameName(s_Space_sbg)
    --_bgBrick:setAnchorPoint(ccp(0.5, 0.5))
    --_bgBrick:setPosition(gdccp(px, py))
    --layer:addChild(_bgBrick)
    --
    --local _batchAchieveNode = CCSpriteBatchNode:create(s_MainUI2)
    --layer:addChild(_batchAchieveNode)
    --
    ----成就图标
    --local _spw = 204
    --local _sph = 230
    --if Achieve_IconDatas[achInfo[PStage]] ~= nil then
    --    spr_icon = CCSprite:createWithSpriteFrameName(Achieve_IconDatas[achInfo[PStage]])
    --    spr_icon:setPosition(gdccp(px - 150, py + 10))
    --    layer:addChild(spr_icon)
    --end
    --        
    ----"成就解锁"
    --local txt = CCLabelTTF:create("成就解锁", "Arial", FontSize + 10)
    --txt:setColor(ccc3(110, 255, 110))
    --txt:setPosition(gdccp(px + 60, py + 100))
    --layer:addChild(txt)
    --
    ----成就名称
    --local txtn = CCLabelTTF:create(achInfo[PName], "Arial", FontSize + 10)
    --txtn:setColor(ccc3(246, 196, 124))
    --txtn:setPosition(gdccp(px + 90, py))
    --layer:addChild(txtn)
    --
    ----提示点击按钮
    --local txtT = CCLabelTTF:create("请在成就栏查看", "Arial", FontSize)
    --txtT:setColor(ccc3(124,150,246))
    --txtT:setPosition(gdccp(px + 150, py - 110))
    --local arr1 = CCArray:create()
    --arr1:addObject(CCFadeTo:create(0.5, 100))
    --arr1:addObject(CCFadeTo:create(0.5, 255))
    --txtT:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
    --layer:addChild(txtT)
    --
    ----显示动画
    --for k, obj in pairs({spr_icon, txt, txtn, txtT}) do
    --    --local array = CCArray:create()
    --    --array:addObject(CCFadeIn:create(1))
    --    --local seq    = CCSequence:create(array)
    --    --obj:runAction(CCSequence:createWithTwoActions(seq, CCCallFuncN:create(unLockBtn)))
    --    
    --    local array = CCArray:create()
    --    array:addObject(CCFadeIn:create(0.5))
    --    array:addObject(CCFadeTo:create(1.5, 200))
    --    array:addObject(CCFadeTo:create(0.5, 0))
    --    --array:addObject(CCFadeOut:create(0.1))
    --    local seq    = CCSequence:create(array)
    --    obj:runAction(seq)
    --end
    --
    --local array = CCArray:create()
    --array:addObject(CCFadeIn:create(0.5))
    --array:addObject(CCFadeTo:create(1.5, 200))
    --array:addObject(CCFadeTo:create(0.5, 0))
    ----array:addObject(CCFadeOut:create(0.1))
    --local seq    = CCSequence:create(array)
    --_bgBrick:runAction(CCSequence:createWithTwoActions(seq, CCCallFuncN:create(unLockBtn)))
    --
    --
    --CCDirector:sharedDirector():getRunningScene():addChild(layer)
    
    --_GameEndRun = true
    
    GFunc_ShowSMessage(string.format("成就解锁:%s",achInfo[PName]))
    
    --音效
    Play_Effect(sme_Achieve)
end


local gMsgSums = 0
GFunc_CreateShowMessage = function(title, msg, btips, scene)
    local layer = CCLayerColor:create(ccc4(0,0,0,0))
    gMsgSums = 0
    local function unLockBtn()
        
        if scene ~= nil then
            scene:removeChild(layer, true)
        else
            CCDirector:sharedDirector():getRunningScene():removeChild(layer, true)
        end
    
        gMsgSums = gMsgSums - 1
    end
    
    --背景
    local px = DWinSizeWidth/2
    local py = DWinSizeHeight/2 + 120
    local _bgBrick = CCSprite:createWithSpriteFrameName(s_Space_sbg)
    _bgBrick:setAnchorPoint(ccp(0.5, 0.5))
    _bgBrick:setPosition(gdccp(px, py))
    layer:addChild(_bgBrick)
            
    --"成就解锁"
    local txt = CCLabelTTF:create(title, "Arial", FontSize10)
    txt:setColor(ccc3(110, 255, 110))
    txt:setPosition(gdccp(px, py + 80))
    layer:addChild(txt)
    
    
    --成就名称
    local txtn = CCLabelTTF:create(msg, "Arial", FontSize10)
    txtn:setColor(ccc3(246, 196, 124))
    txtn:setPosition(gdccp(px, py))
    layer:addChild(txtn)
    
    
    --提示点击按钮
    if btips ~= false then
        local txtT = CCLabelTTF:create("-商店中可查看-", "Arial", FontSize)
        txtT:setColor(ccc3(124,150,246))
        txtT:setPosition(gdccp(px + 150, py - 110))
        local arr1 = CCArray:create()
        arr1:addObject(CCFadeTo:create(0.5, 100))
        arr1:addObject(CCFadeTo:create(0.5, 255))
        txtT:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
        layer:addChild(txtT)
    end
    
    local showt = 2.0 + gMsgSums * 2.0
    
    --显示动画
    for k, obj in pairs({txt, txtn, txtT}) do
        local array = CCArray:create()
        array:addObject(CCFadeIn:create(0.5))
        array:addObject(CCFadeTo:create(showt, 255))
        array:addObject(CCFadeOut:create(0.5))
        --array:addObject(CCFadeOut:create(0.1))
        local seq    = CCSequence:create(array)
        obj:runAction(seq)
    end
    
    local array = CCArray:create()
    array:addObject(CCFadeIn:create(0.5))
    array:addObject(CCFadeTo:create(showt, 255))
    array:addObject(CCFadeOut:create(0.5))
    --array:addObject(CCFadeOut:create(0.1))
    local seq    = CCSequence:create(array)
    _bgBrick:runAction(CCSequence:createWithTwoActions(seq, CCCallFuncN:create(unLockBtn)))
    
    if scene ~= nil then
        scene:addChild(layer, 10 - gMsgSums)
    else
        CCDirector:sharedDirector():getRunningScene():addChild(layer, 10 - gMsgSums)
    end
    
    gMsgSums = gMsgSums + 1
end


local _liwuTable = {
    { item = Item_HpItem, x = 0, y = 39}, 
    { item = Item_TimerUp, x = 40, y = 59}, 
    { item = Item_Wudi, x = 60, y = 74},
    { item = Item_Baozou, x = 75, y = 84},
    { item = Item_Fuhuo, x = 85, y = 99},
}
GFunc_ZhenSong = function(msg)
    --13/2/22 取消
    ----随机赠送一个道具
    --local idx = math.random(0,99)
    --local myItem = nil
    --table.foreach(_liwuTable, function(k,info)
    --    if idx >= info.x and idx <= info.y then
    --        myItem = info.item
    --    end
    --end)
    --if myItem ~= nil then
    --    UpItems(myItem,1)
    --    local title = string.format("获得道具 %s x1", ShopItems_Infos[myItem][PName])
    --    GFunc_CreateShowMessage(msg, title)
    --end
end











